﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class EditCompany : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
    int UserID;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.IsAuthenticated == false)
        {
            Response.Redirect("login.aspx");
        }

        if (Request.IsAuthenticated)
        {
            string n = "";
            UserID = Convert.ToInt32(User.Identity.Name);
            SqlCommand sCmd = new SqlCommand("SELECT name FROM USERS WHERE id=@id", conn);
            sCmd.Parameters.Add(new SqlParameter("@id", SqlDbType.Int)).Value = User.Identity.Name;
            conn.Open();
            SqlDataReader dr = sCmd.ExecuteReader();
            while (dr.Read())
            {
                string name = Convert.ToString(dr[0].ToString());
                n = name;
            }
            dr.Close();
            conn.Close();

            WelcomeBackMessage.Text = "Welcome back, " + n + "!";

            AuthenticatedHeaderPanel.Visible = true;
            AuthenticatedMessagePanel.Visible = true;
            AuthenticatedMenuPanel.Visible = true;

            if (!IsPostBack)
            {
                SetData();
            }
        }
        else
        {
            AuthenticatedHeaderPanel.Visible = false;
            AuthenticatedMessagePanel.Visible = false;
            AuthenticatedMenuPanel.Visible = false;
        }
    }

    public void SetData()
    {
        Response.Write(lbl_user_company_id.Text = Session["User_Company_ID"].ToString());
        Response.Write(lbl_user_id.Text = Session["User_ID"].ToString());
        Response.Write(lbl_company_id.Text = Session["Company_ID"].ToString());

        _User.EditUser(UserID);
        lbl_user.Text = _User.EditUser(UserID)._Name + " " + _User.EditUser(UserID)._SecondName;

        lbl_user_company_id.Text = Convert.ToString(_User_company.Sel_User_company(lbl_company_id.Text)._id_user_company);
        lbl_company_id.Text = Convert.ToString(_User_company.Sel_User_company(lbl_company_id.Text)._id_company);

        tb_comp_name.Text = _User_company.Sel_User_company(lbl_company_id.Text)._company_name;
        tb_comp_reg_nr.Text = _User_company.Sel_User_company(lbl_company_id.Text)._company_reg_nr;
        //cb_comp_vat.Checked = _User_company.Sel_User_company(lbl_company_id.Text)._company_vat;

        check_main_comp.Checked = _User_company.Sel_User_company(lbl_company_id.Text)._main_company;
        tb_position.Text = _User_company.Sel_User_company(lbl_company_id.Text)._position;

        Response.Clear();
    }

    protected void bt_edit_Click(object sender, EventArgs e)
    {
        string user_company = lbl_user_company_id.Text;
        string company = lbl_company_id.Text;
        string user = lbl_user_id.Text;
        string comp_name = tb_comp_name.Text;
        string com_reg_nr = tb_comp_reg_nr.Text;
        string comp_vat = cb_comp_vat.Checked.ToString();
        string main_comp = check_main_comp.Checked.ToString();
        string position = tb_position.Text;
        string client = "false";
        string status = "Active";

        _User_company.Update(user_company, user, company, main_comp, position, comp_name, com_reg_nr, comp_vat, client, status);
        Response.Redirect("MyCompanies.aspx");
    }

    protected void bt_cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("MyCompanies.aspx");
    }
}